package com.mastermind;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.event.WindowEvent;

import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.event.MouseInputListener;

public class MastermindFrame extends JFrame implements ActionListener, MouseInputListener{
	
	/**
	 * Thanks for generating this again, Eclipserino!
	 */
	private static final long serialVersionUID = 4396809023489716758L;
	private MastermindGamePanel panel;
	
	MastermindFrame() {
		
		try {
			UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
		} catch (ClassNotFoundException | InstantiationException
				| IllegalAccessException | UnsupportedLookAndFeelException e) {
		}
		
		setTitle("Mastermind");
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setResizable(false);
		
		setupGame();
		
		setupMenu();
		
		addMouseListener(this);
		addMouseMotionListener(this);
		pack();
		setVisible(true);
	}

	private void setupGame() {	
		MastermindGamePanel panel = new MastermindGamePanel();
		setContentPane(panel);
		this.panel = panel;
		this.panel.setContainer(this);
	}

	private void setupMenu() {
		JMenuBar menuBar = new JMenuBar();
		
		JMenu m = new JMenu("Game Menu");
		m.setMnemonic(KeyEvent.VK_G);

		// game type selection dialog
		JMenuItem i = new JMenuItem("Basic Mastermind");
		m.add(i);
		i = new JMenuItem("Advanced Mastermind");
		i.setEnabled(false);
		m.add(i);
		
		m.addSeparator();
		
		// exit dialog
		i = new JMenuItem("Exit");
		i.addActionListener(this);
		m.add(i);
		
		menuBar.add(m);
		
		setJMenuBar(menuBar);
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		if ("Exit".equals(e.getActionCommand())) {
			int reply = JOptionPane.showConfirmDialog(null, "Are you sure you want to Exit?", "Exit?", JOptionPane.YES_NO_OPTION);
			if (reply == JOptionPane.YES_OPTION) {
				this.dispatchEvent(new WindowEvent(this, WindowEvent.WINDOW_CLOSING));
			}
		}
	}

	@Override
	public void mouseClicked(MouseEvent e) {
		panel.click(e);
	}

	@Override
	public void mousePressed(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseReleased(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseEntered(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseExited(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseDragged(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseMoved(MouseEvent e) {
		panel.mouse(e);		
	}
}
